package com.szit.deviceclient.controller;

import com.szit.deviceclient.feign.UserFeign;
import com.szit.deviceclient.remote.AdminFeign;
import com.szit.util.CodeMsg;
import com.szit.util.Result;
import com.szit.util.vo.DeviceUserInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wyler
 * @date 2020-10-15
 */

@Slf4j
@RestController
public class DeviceClientController {

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private AdminFeign adminFeign;

    @ApiOperation(value = "后台添加设备用户" ,notes = "后台添加设备用户")
    @PostMapping("/deviceUser")
    public Result addDeviceUser(@RequestBody DeviceUserInfo deviceUserInfo){
        log.info("device-client: addDeviceUser");
        log.info("-----selectAdminUserByToken------");
        Result result = adminFeign.selectAdminUserByToken(deviceUserInfo.getToken());
        if(result.getRetCode() == 200){
            /*因json反序列化问题导致原Long参数被自动转成Integer*/
            Integer cOrder = (Integer) result.getData();
            if(cOrder > 3 ){
                log.info("--------当前后台用户权限不足---------");
                return Result.error(CodeMsg.NO_PERMISSIONS);
            }
        }else {
            log.info("--------错误信息直接返回---------");
            return result;
        }
        return userFeign.insertDeviceUser(deviceUserInfo);
    }

    @ApiOperation(value = "后台删除设备用户" ,notes = "后台删除设备用户")
    @DeleteMapping("/deviceUser")
    public Result deleteDeviceUser(DeviceUserInfo deviceUserInfo){
        log.info("device-client: deleteDeviceUser");
        log.info("-----selectAdminUserByToken------");
        Result result = adminFeign.selectAdminUserByToken(deviceUserInfo.getToken());
        if(result.getRetCode() == 200){
            /*因json反序列化问题导致原Long参数被自动转成Integer*/
            Integer cOrder = (Integer) result.getData();
            if(cOrder > 3 ){
                log.info("--------当前后台用户权限不足---------");
                return Result.error(CodeMsg.NO_PERMISSIONS);
            }
        }else {
            log.info("--------错误信息直接返回---------");
            return result;
        }
        return userFeign.deleteDeviceUser(deviceUserInfo);
    }

    @ApiOperation(value = "后台修改设备用户" ,notes = "后台修改设备用户")
    @PutMapping("/deviceUser")
    public Result updateDeviceUser(@RequestBody DeviceUserInfo deviceUserInfo){
        log.info("device-client: updateDeviceUser");
        log.info("-----selectAdminUserByToken------");
        Result result = adminFeign.selectAdminUserByToken(deviceUserInfo.getToken());
        if(result.getRetCode() == 200){
            /*因json反序列化问题导致原Long参数被自动转成Integer*/
            Integer cOrder = (Integer) result.getData();
            if(cOrder > 3 ){
                log.info("--------当前后台用户权限不足---------");
                return Result.error(CodeMsg.NO_PERMISSIONS);
            }
        }else {
            log.info("--------错误信息直接返回---------");
            return result;
        }
        return userFeign.updateDeviceUser(deviceUserInfo);
    }

    @ApiOperation(value = "后台查询所有设备用户" ,notes = "后台查询所有设备用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "token",dataType = "String",required = true),
            @ApiImplicitParam(name = "pageNum",value = "当前页",dataType = "int",required = false,defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页个数",dataType = "int",required = false,defaultValue = "10")
    })
    @GetMapping("/deviceUser")
    public Result selectDeviceUser(String token,Integer pageNum,Integer pageSize){
        log.info("device-client: selectAllDeviceUser");
        log.info("-----selectAdminUserByToken------");
        Result result = adminFeign.selectAdminUserByToken(token);
        if(result.getRetCode() == 200){
            /*因json反序列化问题导致原Long参数被自动转成Integer*/
            Integer cOrder = (Integer) result.getData();
            if(cOrder > 4 ){
                log.info("--------当前后台用户权限不足---------");
                return Result.error(CodeMsg.NO_PERMISSIONS);
            }
        }else {
            log.info("--------错误信息直接返回---------");
            return result;
        }
        return userFeign.selectAllDeviceUser(pageNum,pageSize);
    }
}
